import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/authentication.dart';
import 'package:ichinesereader/controller/cache.dart';

class XLogin extends StatelessWidget {
  XLogin({Key? key}) : super(key: key);
  static const String routeName = '/xlogin';
  final LoginController _logincontrol = Get.find();
  final box = Hive.box('appconfig');

  void initializeSettings() async {
    //AuthMessage _authMessage =
    await _logincontrol.checkLoginStatus().then((_authMessage) {
      if (_authMessage.status == LoginStatusKey.logged_in) {
        String role = box.get(CacheManagerKey['nanhairole'].toString());
        switch (role) {
          case Constants.NanhaiRole_Student:
            {
              Get.offAllNamed('/student/home');
              break;
            }
          case Constants.NanhaiRole_Teacher:
            {
              Get.offAllNamed('/teacher/home');
              break;
            }
          case Constants.NanhaiRole_SchoolAdmin:
            {
              Get.offAllNamed('/schooladmin/home');
              break;
            }
          case Constants.NanhaiRole_Individual:
            {
              Get.offAllNamed('/individual/home');
              break;
            }
          case Constants.NanhaiRole_Parent:
            {
              Get.offAllNamed('/parent/home');
              break;
            }
          default:
            Get.offAllNamed('/error');
            break;
        }
      } else if (_authMessage.status == LoginStatusKey.has_placement) {
        Get.offAllNamed('/placement', arguments: true);
      } else {
        Get.offAndToNamed('/login');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    initializeSettings();
    return Center(
        child: CircularProgressIndicator(
      backgroundColor: Get.theme.primaryColorDark,
      strokeWidth: 9.0,
    ));
  }
}
